package com.woniuxy.special.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.special.entity.SysSpecialUser;
import com.woniuxy.special.entity.SysUserInfo;
import com.woniuxy.special.from.SpecialRegisterFrom;
import com.woniuxy.special.mapper.SysSpecialUserMapper;
import com.woniuxy.special.mapper.SysUserInfoMapper;
import com.woniuxy.special.service.SysSpecialUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.special.utils.OSSUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2025年04月07日
 */
@Service
public class SysSpecialUserServiceImpl extends ServiceImpl<SysSpecialUserMapper, SysSpecialUser> implements SysSpecialUserService {
    @Resource
    private SysSpecialUserMapper sysSpecialUserMapper;
    @Resource
    private SysUserInfoMapper sysUserInfoMapper;
    @Override
    public void addRegister(SpecialRegisterFrom from, Integer adminId) throws IOException {
     //判断是否存在
        SysSpecialUser sysSpecialUser = sysSpecialUserMapper.selectById(from.getPhone());
            if (sysSpecialUser != null)  throw new RuntimeException("该用户已存在");
        //头像后缀
        String suffix = from.getFile().getOriginalFilename().substring(from.getFile().getOriginalFilename().lastIndexOf(".") );
        //生成新文件名
        String fileName= UUID.randomUUID().toString()+suffix;
        String url = OSSUtil.uploadFile(from.getFile().getInputStream(), fileName, "woniu117b");
        SysSpecialUser specialUser = BeanUtil.toBean(from, SysSpecialUser.class);
        specialUser.setHeadImg(url);
        specialUser.setCreateUserId(adminId+"");
        //获取一个当前时间的localDateTime
        specialUser.setCreateTime(LocalDateTime.now());
        //生成特派员
        sysSpecialUserMapper.insert(specialUser);
        //生成用户信息
        SysUserInfo userInfo = new SysUserInfo();
        userInfo.setCreateUserId(adminId+"");
        userInfo.setCreateTime(LocalDateTime.now());
        userInfo.setSpecialId(specialUser.getId());
        userInfo.setLoginName(from.getPhone());
        userInfo.setPhone(from.getPhone());
        userInfo.setLoginPwd("123456");
        userInfo.setType("1");

        sysUserInfoMapper.insert(userInfo);
    }
}
